<?php session_start(); ?>
<?php include_once('misc/class_config.php'); ?>

<?php 
	include('access_control/page_access.php');
	
	setPreviousPage();
	declinePageIfNotLoggedIn();
	declinePageIfAdmin();
	$currentUser = new user();
	$currentUser->isLoggedIn();
	$currentUser->isTimedOut();
	
	$existingProduct = new product();
	$userCart = new UserCartItem();
	
	$cartMessage = "";
	$actionSuccess = false;
	$actionMessage = "";
?>

<?php
	if($currentUser->loggedIn)
	{
		if(!$currentUser->timedOut)
		{
			try
			{
				include_once "controller/cartcontroller.php";
			}
			catch(Exception $ex)
			{
				$cartMessage = $ex->getMessage();
			}
		}
	}
?>

<?php $pageTitle = $currentUser->firstName."'s Shopping Cart | Everything"; ?>
<?php include('layout/page_start.php'); ?>
<?php include('layout/page_header.php'); ?>
<?php include('layout/menu_bar.php'); ?>

<div id="main_content_div">

<?php
	echo $actionMessage;
	if($currentUser->loggedIn)
	{
		if($currentUser->timedOut)
		{
			include_once "layout/timeout.php";
			$currentUser->timeoutUser();
		}
		else
		{
			echo '<div class="page_message_div"><a>'.$cartMessage.'</a></div>';
			$userCart->userId = $currentUser->id;
			$allCartItems = $userCart->getAllItems();
			if($allCartItems)
			{	
				echo '<div id="checkout_button"><a href="cartcheckout.php"><img width="150px" height="55px" src="images/misc_icon/green_checkout_button.png"/></a></div>';
				foreach($allCartItems as $resultSet)
				{
					include "layout/cart_item_display.php";
				}
				
			}
			else
			{
				$cartMessage = "<span>Your shopping cart is empty.</span>";
				echo '<div class="page_message_div"><p>'.$cartMessage.'</p></div>';
			}
		}
	}
?>
</div>

<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>
<?php setCurrentPage();?>